package es.ucm.fdi.isbc.viviendas.representacion.similitud;

import jcolibri.exception.NoApplicableSimilarityFunctionException;
import jcolibri.method.retrieve.NNretrieval.similarity.LocalSimilarityFunction;

public class SimilitudLocalizacion implements LocalSimilarityFunction{

	public double compute(Object caseObject, Object queryObject)
			throws NoApplicableSimilarityFunctionException {
		
		if(caseObject==null || queryObject==null){
			return 0.0;
		}

		double valor=0;
		
		int p1=0,p2=0,iguales=0,totales=0;
		
		//si los string son iguales, no recorremos los arboles como tontos
		if(caseObject.equals(queryObject)){
			valor=1;
		}else{
			//Iniciamos los punteros de los string
			if(((String)caseObject).length()>0){
				while(((String)caseObject).charAt(p1)!='/' && p1<((String)caseObject).length()){
					p1++;
				}
			}
			if(((String)queryObject).length()>0){
				while(((String)queryObject).charAt(p2)!='/' && p2<((String)queryObject).length()){
					p2++;
				}
			}
			
			String cadena1=((String)caseObject).substring(p1);
			String cadena2=((String)queryObject).substring(p2);
			
			String[] cadenas1=cadena1.split("/");
			String[] cadenas2=cadena2.split("/");
			
			int long1=cadenas1.length;
			int long2=cadenas2.length;
			
			//comparamos los string por tags
						
			int i=0;
			boolean fail=false;
			while(!fail && i<Math.min(long1, long2)){
				if(cadenas1[i].equals(cadenas2[i])){
					iguales++;
					i++;
				}else{
					fail=true;
				}				
			}
			
			totales=Math.max(long1, long2);
						
			//asignamos similitud
			valor=iguales/totales;
		}
				
		return valor;
	}


	public boolean isApplicable(Object caseObject, Object queryObject) {
		return (caseObject instanceof String && queryObject instanceof String);
	}

}
